package com.groupfour.attendancemanagementsystem.controller;
/**
 * @version: V1.0
 * @author: 章珑赢
 * @className: PersonaRatingService
 * @packageName: controller
 * @description: 个人评分
 * @data: 2022/4/23
 */

import com.groupfour.attendancemanagementsystem.entity.DTO.CourseAndGroupDTO;
import com.groupfour.attendancemanagementsystem.entity.VO.HomeWorkDetailsVO;
import com.groupfour.attendancemanagementsystem.entity.VO.PersonaRatingVO;
import com.groupfour.attendancemanagementsystem.entity.VO.PersonalDetailVO;
import com.groupfour.attendancemanagementsystem.service.impl.PersonaRatingServiceImpl;
import com.groupfour.attendancemanagementsystem.utils.JwtHelper;
import com.groupfour.attendancemanagementsystem.utils.Result;
import com.groupfour.attendancemanagementsystem.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
@RequestMapping("/personalRating")
public class PersonalRatingController {

    @Autowired
    private PersonaRatingServiceImpl personaRatingService;

    final private Integer pageSize=10;

    /**
     * @version: V1.0
     * @author 章珑赢
     * @methodsName SelectAllPersonaRating
     * @description 查询所有个人评分
     * @param pageNum,userCode
     * @return Result
     **/
    @GetMapping("/SelectAllPersonalRating/{pageNum}")
    @ResponseBody
    public Result SelectAllPersonaRating(
            @PathVariable Integer pageNum,
            @RequestHeader("token")String token
    ){
        boolean expiration= JwtHelper.isExpiration(token);
        if (expiration){
            return Result.build(null, ResultCodeEnum.TOKEN_ERROR);
        }
        //        从token中解析出用户的学生学号
        String userCode = JwtHelper.getUserCode(token);
        Integer userType = JwtHelper.getUserType(token);


        List<PersonaRatingVO> personaRatingVO = null;
        switch (userType){
            case 0:
                personaRatingVO= personaRatingService.SelectAllPersonaRating(pageNum,pageSize,"");
                break;
            case 2:
                personaRatingVO= personaRatingService.SelectAllPersonaRating(pageNum,pageSize,userCode);
                break;
        }

        if(personaRatingVO != null){
            return Result.ok(personaRatingVO);
        }
        else {
            return Result.fail().message("出错了");
        }
    }
    /**
     * @version: V1.0
     * @author: 章珑赢
     * @methodsName: SelectByProjectNameOrGroupName
     * @description: 根据项目名称或小组名称查询所有小组评分
     * @param:  projectName(项目名称)，groupName(小组评分),token(用户编号)
     * @return: Result
     * @throws: 无
     **/
    @RequestMapping("/SelectPersonalByProjectNameOrGroupName/{pageNum}")
    @ResponseBody
    public Result SelectPersonalByProjectNameOrGroupName(@RequestBody CourseAndGroupDTO courseAndGroup, @PathVariable Integer pageNum, @RequestHeader("token") String token) {
        boolean expiration = JwtHelper.isExpiration(token);
        if (expiration) {
        return Result.build(null, ResultCodeEnum.TOKEN_ERROR);
    }
    //        从token中解析出用户的学生学号
    String userCode = JwtHelper.getUserCode(token);
    Integer userType = JwtHelper.getUserType(token);
        System.out.println(courseAndGroup.getCourse());
        System.out.println(courseAndGroup.getGroup());

    List<PersonaRatingVO> personaRatingVO = null;
        switch (userType) {
        case 0:
            personaRatingVO = personaRatingService.SelectByProjectNameOrGroupName(courseAndGroup.getCourse(), courseAndGroup.getGroup(), pageNum, pageSize, "");
            break;
        case 2:
            personaRatingVO = personaRatingService.SelectByProjectNameOrGroupName(courseAndGroup.getCourse(), courseAndGroup.getGroup(), pageNum, pageSize, userCode);
            break;
    }
            if (personaRatingVO != null) {
        return Result.ok(personaRatingVO);
    } else {
        return Result.fail().message("出错了");
    }
}
    /**
     * @version: V1.0
     * @author: 章珑赢
     * @methodsName: SelectGroupDetails
     * @description: 查询个人的详情
     * @param:  studentCode（学生学号），token(小组编号)
     * @return: Result
     * @throws: 无
     **/
    @RequestMapping("/SelectPersonalDetails")
    @ResponseBody
    public Result SelectPersonalDetails(String studentCode,String groupCode ){
        PersonalDetailVO personalDetailVO = personaRatingService.SelectPersonalDetails(studentCode,groupCode);
        if (personalDetailVO!= null) {
            return Result.ok(personalDetailVO);
        } else {
            return Result.fail().message("出错了");
        }
    }
    @RequestMapping("/SelectPersonalHomeWorks/{pageNum}")
    @ResponseBody
    public Result SelectPersonalHomeWorks(String studentCode, @PathVariable Integer pageNum ,String groupCode){
        List<HomeWorkDetailsVO> homeWorkDetailsVOS=personaRatingService.SelectHomeWorks(studentCode,pageNum,pageSize,groupCode);
        if (homeWorkDetailsVOS!=null) {
            return Result.ok(homeWorkDetailsVOS);
        } else {
            return Result.fail().message("出错了");
        }
    }

    /**
     * @version: V1.0
     * @author: 章珑赢
     * @methodsName: DoPersonalGradeAndComment
     * @description: 进行小组打分和评论
     * @param:  studentCode（学生学号），oneselfSyntnesisScore（个人综合分数），oneselfComment（个人评论）
     * @return: Result实体类
     * @throws: 无
     **/
    @RequestMapping("/DoPersonalGradeAndComment/{studentCode}")
    @ResponseBody
    public Result DoPersonalGradeAndComment(Integer oneselfSyntnesisScore, String oneselfComment,  @PathVariable String studentCode){
        boolean success=personaRatingService.DoPersonalGradeAndComment(oneselfSyntnesisScore,oneselfComment,studentCode);
        if(success){
            return Result.ok();
        }
        return Result.fail("保存失败");
    }
}
